package net.daum.hibernate.sample;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

import com.google.gson.annotations.Expose;

@Entity(name = "car_make")
public class Make {
	@Column
	@Id
	@GeneratedValue
	@Expose
	private Integer makeid;

	@Column
	@Expose
	private String makename;

	@Column
	@Expose
	private String makeengname;
	
	@OneToMany(mappedBy = "make", fetch = FetchType.LAZY)	
	@Expose
	private List<Brand> brandList;
	
	public Integer getMakeid() {
		return makeid;
	}


	public void setMakeid(Integer makeid) {
		this.makeid = makeid;
	}


	public String getMakename() {
		return makename;
	}


	public void setMakename(String makename) {
		this.makename = makename;
	}


	public String getMakeengname() {
		return makeengname;
	}


	public void setMakeengname(String makeengname) {
		this.makeengname = makeengname;
	}

	public List<Brand> getBrandList() {
		return brandList;
	}


	public void setBrandList(List<Brand> brandList) {
		this.brandList = brandList;
	}

	public static void main(String[] args) {
		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure("net/daum/hibernate/sample/hibernate.cfg.xml")
				.buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		List makeList = session.createQuery("from car_make").list();
		for (int i = 0; i < makeList.size(); i++) {
			Make make = (Make) makeList.get(i);
			if (make.getMakeid() == 57) {
				/*
				List<Brand> brandList = make.getBrandList();
				for (Brand brand : brandList) {
					if (brand.getBrandid() == 403) {
						
						List<Model> modelList = brand.getModelList();
						Util.print(modelList.get(0).getImageList());
					}
				}
				*/
				
				Brand b = (Brand) session.get(Brand.class, 403);
				b.setBrandname("아반떼2");
				
				session.saveOrUpdate(b);
				
				
			}
		}
		
		
		
		session.getTransaction().commit();
		session.close();
	}
}
